home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12332 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Question on 'bubble sort'
  5. Date: Sat, 30 Mar 96 17:29:18 GMT
  6. Organization: none
  7. Message-ID: <828206958snz@genesis.demon.co.uk>
  8. References: <4jieso$juc@lantana.singnet.com.sg>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4jieso$juc@lantana.singnet.com.sg>
  15.            s8700055@singnet.com.sg "XY Xie" writes:
  16.  
  17. >I came across this sorting algorithm called 'bubble sort' in a book.
  18.  
  19. If the book doesn't explain why there is never a good reason to use bubble
  20. sort then I suggest you get another book.
  21.  
  22. >Here is a part of the book's code for sorting an array of integers in
  23. >ascending order :
  24. >
  25. >for(outer=0 ; outer<(NUM-1) ; outer++)
  26. >{
  27. >  //variable definitions here......
  28.  
  29. If that book suggests that you use // in C code then definitely get a
  30. different one. // is a syntax error in C.
  31.  
  32.  
  33. >  int didswap = 0;   //didswap remains zero if no swap occurs below
  34. >                             //my question is on 'didswap'
  35. >
  36. >for (inner=outer ; inner< NUM ; inner++)
  37. > {
  38. >   if ( arrayname[outer] > arrayname[inner])
  39. >   {
  40. >     //code that swaps values follows
  41. >     didswap = 1   //didswap = 1 if a swap took place
  42. >   }
  43. > }
  44. >if ( ! didswap)  //*returns 1 if swap took place*(relates to question)
  45. >
  46. >  { break ; }     //outer for loop terminated
  47. >
  48. >}
  49. >
  50. >My QUESTION : if the smallest integer happens to be the first member
  51. >initially , would not the 'if ( !didswap ) '  test terminate the
  52. >sorting procedure prematurely , with the rest of the list unsorted ?
  53. >If not , why ?
  54.  
  55. didswap will be set to 1 if any swap occurred in the inner loop. If the
  56. first element is the smallest didswap won't be set to 1 on the first
  57. iteration of the inner loop but it may still be set on subsequent iterations.
  58. if (!didswap) would only succeed if no swaps occurred.
  59.  
  60. >Someone pls enlighten me . Pls email me direct . THANKs for the
  61. >trouble . 
  62.  
  63. If you post to a newsgroup have the courtesy to read the responses there
  64. so that others can benefit from them and comment on them (that is in your
  65. interest).
  66.  
  67. -- 
  68. -----------------------------------------
  69. Lawrence Kirby | fred@genesis.demon.co.uk
  70. Wilts, England | 70734.126@compuserve.com
  71. -----------------------------------------
  72.